package com.lyt.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lyt.bean.LinkAnswer;
import com.lyt.bean.TestAnswer;
import com.lyt.dao.TestAnswerDao;
import com.lyt.util.BeanUtil;
import com.lyt.vo.LinkAnswerVo;
import com.lyt.vo.RetAnswerVo;
import com.lyt.vo.TestAnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

/**
 * @Author:张勇
 * @Date：2023/5/18 10:47
 * @Info:
 */
@Service
public class TestAnswerService {

    @Autowired
    TestAnswerDao dao;

    //考试提交答案------->已完成
    //先进来查该学生是否已经提交过答案 提交过则将linkAnswers中 关联表id查出循环进行删除
    //再进行插入linkAnswer表中 然后更新testAnswer表中的答案数据
    public Integer addTestStatus(TestAnswerVo testAnswerVo) {
        String retTestAnswer = dao.selLinkAnswerByStuIdAndPaperId(testAnswerVo.getStu_test_id(),testAnswerVo.getPaper_id());

        JSONArray retLinkAnswer = JSON.parseArray(retTestAnswer);
        System.out.println("我要看到你"+retLinkAnswer);
        if(retLinkAnswer != null){
            for (Object ignored : retLinkAnswer){
                Integer num = delAnswerById((Integer) ignored);
            }
        }

        System.out.println("---------------111----------------");
        TestAnswer testAnswer = new TestAnswer();
        TestAnswer newTestAnswer = BeanUtil.beanConvert(testAnswerVo, testAnswer);
        List<LinkAnswer> linkAnswers = new ArrayList<>();
        for(LinkAnswerVo linkAnswerVo: testAnswerVo.getLinkAnswerVos()){
            LinkAnswer linkAnswer = new LinkAnswer();
            linkAnswer.setIndex(linkAnswerVo.getIndex());
            linkAnswer.setQuestion_id(linkAnswerVo.getQuestion_id());
            linkAnswer.setAnswer(JSON.toJSONString(linkAnswerVo.getAnswer()));
            linkAnswers.add(linkAnswer);
        }
        //将答案id取出来放在数组中
        Integer[] arr = new Integer[linkAnswers.size()];
        int index = 0 ;
        for (LinkAnswer linkAnswer : linkAnswers){
            dao.addLinkAnswer(linkAnswer);
            //新插入的数据id会被封装在linkAnswer对象的id中
            arr[index] = linkAnswer.getId();
            index++;
        }
        System.out.println(JSON.toJSONString(arr));
        testAnswer.setLinkAnswers(JSON.toJSONString(arr));

        //找到试卷答案id  先表中是否存在已有答案的数据 存在的话就更新 否则就直接添加
        System.out.println("----------------222----------------");
        Integer retTestAnswerId = dao.selTestAnswerIdByStuIdAndPaperId(testAnswerVo.getStu_test_id(),testAnswerVo.getPaper_id());
        System.out.println("找到testAnswer表中的Id"+retTestAnswerId);
        if(retTestAnswerId != null){
            dao.updateAnswer(JSON.toJSONString(arr),retTestAnswerId);
        }else {
            dao.addAnswer(testAnswer);
        }
        return 1;
}


    //查答案----》已完成
    //先查出该学生已添加的答案linkAnswer中存放的linkAnswer ID 然后找出关联表中某个学生答的某张试卷的所有答案
    public List<RetAnswerVo> selAnswerById(Integer stu_id,Integer paper_id){
        List<LinkAnswer> list = new ArrayList<>();

        String retTestAnswer = dao.selLinkAnswerByStuIdAndPaperId(stu_id,paper_id);
        JSONArray retLinkAnswer = JSON.parseArray(retTestAnswer);
        if(retLinkAnswer != null){
            for (Object ignored : retLinkAnswer){
                LinkAnswer linkAnswer = dao.selAnswerById((Integer) ignored);
                list.add(linkAnswer);
            }
        }
        List<RetAnswerVo> voList = new ArrayList<>();
        for (LinkAnswer linkAnswer:list){
            RetAnswerVo linkAnswerVo = new RetAnswerVo();
            linkAnswerVo.setIndex(linkAnswer.getIndex());
            linkAnswerVo.setQuestion_id(linkAnswer.getQuestion_id());
            linkAnswerVo.setAnswer(linkAnswer.getAnswer());
            voList.add(linkAnswerVo);
        }
        return voList;
    }

    //删除关联表中某个学生答的某张试卷的所有答案--》修改不如删除
    public Integer delAnswerById(Integer id){
        Integer num = dao.delAnswerById(id);
        return num;
    }
}